<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test5</title>
</head>
<body>
<h1 style="text-align: center">HTML复习历程-5</h1>
<p>JavaScript闭包：</p>
<p id="count1">count:0</p>
<button type="button" onclick="click1()">增加</button>
<br>
<br>
<hr>
<h3 style="text-align: center">Date相关</h3>
<button type="button" onclick="createList()">查看</button>
<div id="time_list"></div>
<button type="button" onclick="anyNum()">随机数：</button>
<p id="suiji"></p>
<script>
    var add = (function () {
        var count = 0
        return function (){
            return ++count
        }
    })()
    function click1() {
        var count1 = document.getElementById("count1")
        count1.innerText = "count:"+add()
    }
    Date.prototype.format = function(fmt){
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o){
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(
                    RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    function createList() {
        var mList = ["yyyy-MM-dd hh:mm:ss","yyyy年MM月dd日hh小时mm分ss秒"]
        var timeList = document.getElementById("time_list")
        var ul = document.createElement("ul")
        timeList.appendChild(ul)
        var now = new Date()
        for (item in mList){
            var li = document.createElement("li")
            li.innerText = now.format(mList[item])
            ul.appendChild(li)
        }
    }

    function anyNum() {
        var sui = document.getElementById("suiji")
        sui.innerText = Math.random()
    }
</script>
</body>
</html>
